﻿Imports Commander_Wars.Grafikengine
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX
Public Class Spieletools
    Public Shared Function Schreiben(ByVal Zeichenkette As String, ByVal Index As Integer, ByVal Neutxt As String) As String
        Zeichenkette = Schreiben(Zeichenkette, Index, Neutxt, "_")
        Return Zeichenkette
    End Function

    Public Shared Function Schreiben(ByVal Zeichenkette As String, ByVal Index As Integer, ByVal Neutxt As String, ByVal Trennzeichen As String) As String
        If Zeichenkette = "" Then
            Return ""
        Else
            Dim sAntwort As String()
            Dim sTrennzeichen As Char()

            sTrennzeichen = Trennzeichen
            sAntwort = Zeichenkette.Split(sTrennzeichen)
            If sAntwort.Length > Index Then
                'sAntwort(Index) = Neutxt

                Dim Max As Integer
                Max = sAntwort.Length
                If Index <= Max Then
                    If 0 < Max Then
                        Zeichenkette = uSchreiben(sAntwort(0), 0, Index, Neutxt)
                    End If
                    Dim oft As Integer
                    Dim I As Integer = 1
                    For oft = 1 To (Max - 1)
                        If I < Max Then
                            Zeichenkette += sTrennzeichen + uSchreiben(sAntwort(I), I, Index, Neutxt)
                        End If
                        I = I + 1
                    Next
                    Return Zeichenkette
                Else
                    Return Nothing
                End If
            Else
                Dim Überschuss As Integer
                Überschuss = Index - sAntwort.Length
                Dim I As Integer
                For I = 0 To Überschuss
                    Zeichenkette += Trennzeichen
                Next
                Return Schreiben(Zeichenkette, Index, Neutxt, Trennzeichen)
            End If
        End If
    End Function
    Private Shared Function uSchreiben(ByVal Textteil As String, ByVal Indextxt As Integer, ByVal Index As Integer, ByVal Neutxt As String)
        Select Case Indextxt
            Case Is < Index
                Return Textteil
            Case Is > Index
                Return Textteil
            Case Is = Index
                Return Neutxt
            Case Else
                Return ""
        End Select
    End Function




    Public Shared Function getBtnZeile(ByVal ButtonTag As String) As String
        Return Splitten(ButtonTag, 2)
    End Function
    Public Shared Function getBtnZeileNum(ByVal ButtonTag As String) As Integer
        Dim Zeile As New Integer

        Zeile = getBtnZeile(ButtonTag)
        Return Zeile
    End Function


    Public Shared Function getBtnSpalte(ByVal ButtonTag As String) As String
        Return Splitten(ButtonTag, 3)
    End Function
    Public Shared Function getBtnSpalteNum(ByVal ButtonTag As String) As Integer
        Dim Zeile As New Integer

        Zeile = getBtnZeile(ButtonTag)
        Return Zeile
    End Function



    Public Shared Function Splitten(ByVal Zeichenkette As String, ByVal Index As Integer) As String
        Return Splitten(Zeichenkette, Index, "_")
    End Function
    Public Shared Function Splitten(ByVal Zeichenkette As String, ByVal Index As Integer, ByVal Trennzeichen As String) As String
        Dim sAntwort As String()
        Dim sTrennzeichen As Char()
        If Zeichenkette = "" Then
            Return ""
        Else
            sTrennzeichen = Trennzeichen
            sAntwort = Zeichenkette.Split(sTrennzeichen)
            If sAntwort.Length > Index Then
                'If sAntwort.Length >= Index Then
                Return sAntwort(Index)
                'Else
                'Return Nothing
                'End If
            Else
                Return "Zu Hoher Index"
            End If
        End If
    End Function

    Private Shared Zufall As Random
    Public Shared Function Zufallszahl(ByVal min As Integer, ByVal max As Integer) As Integer

        If Zufall Is Nothing Then
            Randomize()
            Zufall = New Random
        End If
        Dim X2 As Integer = 0
        If min < 0 Then
            X2 = min
            max = max - min
            min = 0
        End If

        Dim Zahl As Integer '3-5
        Zahl = Math.Round((Zufall.NextDouble * (max - min + 1)), System.MidpointRounding.ToEven) + min
        If Zahl > max Then Zahl = max
        If Zahl < min Then Zahl = min
        Zahl = Zahl + X2
        Return Zahl
    End Function

    ''' <summary>
    ''' Stellt einen eindimensionalen Array zweidimensional dar
    ''' </summary>
    ''' <param name="X">X Position im Array beginnt mit 0</param>
    ''' <param name="Y">Y Position im Array beginnt mit 0</param>
    ''' <param name="Breite">Die Konstantebreite des Array beginnend mit 1</param>
    ''' <returns>Gibt die Stelle im eindimensionalen Array zurück</returns>
    ''' <remarks></remarks>
    Public Shared Function Arraystelle(ByVal X As Integer, ByVal Y As Integer, ByVal Breite As Integer)
        Dim Stelle As Integer = (Y * Breite) + X
        Return Stelle
    End Function

    ''' <summary>
    ''' Gibt alle Vorkommenden Teile des Arrays zurück
    ''' </summary>
    ''' <param name="Element">Der Zu durchsuchende Array</param>
    ''' <param name="Parameter">Der gesuchte Parameter</param>
    ''' <param name="GenauGleich">Muss der String identisch oder nur ähnlich sein</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function FindallElements(ByVal Element() As String, ByVal Parameter As String, Optional ByVal GenauGleich As Boolean = False) As Integer()
        Dim I() As Integer = {}
        Dim I0 As Integer = 0
        For Each Name In Element
            If GenauGleich = False Then
                If Name.Contains(Parameter) = True Then
                    Array.Resize(I, I.Length + 1)
                    I(I.Length - 1) = I0
                End If
            Else
                If Name = Parameter Then
                    Array.Resize(I, I.Length + 1)
                    I(I.Length - 1) = I0
                End If
            End If
            I0 += 1
        Next
        Return I
    End Function
    ''' <summary>
    ''' Gibt das erste Vorkommende Teil des Arrays zurück
    ''' </summary>
    ''' <param name="Arra">Der Zu durchsuchende Array</param>
    ''' <param name="Parameter">Der gesuchte Parameter</param>
    ''' <param name="GenauGleich">Muss der String identisch oder nur ähnlich sein</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function FindElement(ByVal Arra() As String, ByVal Parameter As String, Optional ByVal GenauGleich As Boolean = False) As Integer
        For I = 0 To Arra.Length - 1
            If GenauGleich = False Then
                If Arra(I).Contains(Parameter) = True Then
                    Return I
                End If
            Else
                If Arra(I) = Parameter Then
                    Return I
                End If
            End If
        Next
        Return -1
    End Function
    Public Shared Function IstGerade(ByVal Zahl As Integer) As Boolean
        Dim Rest As Integer
        Math.DivRem(Zahl, 2, Rest)
        If Rest = 1 Then
            Return False
        Else
            Return True
        End If
    End Function
    ''' <summary>
    ''' Erstellt ein Parallelogramm(Kreis in Feldern
    ''' </summary>
    ''' <param name="Radius">Der maximale Abstand eines Feldes (0=1 Feld große Rückgabe)</param>
    ''' <returns>Gibt einen Array zurück in dem -1 für nicht Ereichbar steht und der 2*Radius+1 groß ist in Höhe und Breite</returns>
    ''' <remarks></remarks>
    Public Shared Function Kreisfunktion(ByVal Radius As Integer) As Integer(,)
        Dim Quadrat(,) As Integer = {}
        If Radius >= 0 Then
            ReDim Quadrat(Radius * 2, Radius * 2)
        Else
            ReDim Quadrat(-1, -1)
        End If
        For X = 0 To Radius * 2
            For Y = 0 To Radius * 2
                Quadrat(X, Y) = -1
            Next
        Next
        If Quadrat.Length > 0 Then
            Quadrat(Radius, Radius) = 0
        End If
        For MomentanterRadius = 1 To Radius
            Dim StartX As Integer = Radius
            Dim StartY As Integer = Radius - MomentanterRadius
            For I = 1 To MomentanterRadius
                StartX += 1
                StartY += 1
                Quadrat(StartX, StartY) = MomentanterRadius
            Next
            For I = 1 To MomentanterRadius
                StartX -= 1
                StartY += 1
                Quadrat(StartX, StartY) = MomentanterRadius
            Next
            For I = 1 To MomentanterRadius
                StartX -= 1
                StartY -= 1
                Quadrat(StartX, StartY) = MomentanterRadius
            Next
            For I = 1 To MomentanterRadius
                StartX += 1
                StartY -= 1
                Quadrat(StartX, StartY) = MomentanterRadius
            Next
        Next
        Return Quadrat
    End Function
    ''' <summary>
    ''' Gibt die äußersten Felder eines Kreises zurück 
    ''' </summary>
    ''' <param name="Radius">Der Radius des Kreises</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Kreisfelder(ByVal Radius As Integer) As Point()
        Dim Felder() As Point = {}
        If Radius = 0 Then
            Felder = New Point() {New Point}
        Else

            Dim StartX As Integer = -Radius
            Dim StartY As Integer = 0
            For I = 1 To Radius
                StartX += 1
                StartY -= 1
                Array.Resize(Felder, Felder.Length + 1)
                Felder(Felder.Length - 1) = New Point(StartX, StartY)
            Next
            For I = 1 To Radius
                StartX += 1
                StartY += 1
                Array.Resize(Felder, Felder.Length + 1)
                Felder(Felder.Length - 1) = New Point(StartX, StartY)
            Next
            For I = 1 To Radius
                StartX -= 1
                StartY += 1
                Array.Resize(Felder, Felder.Length + 1)
                Felder(Felder.Length - 1) = New Point(StartX, StartY)
            Next
            For I = 1 To Radius
                StartX -= 1
                StartY -= 1
                Array.Resize(Felder, Felder.Length + 1)
                Felder(Felder.Length - 1) = New Point(StartX, StartY)
            Next
        End If
        Return Felder
    End Function
    Public Shared Function Rechteck(ByVal Länge As Integer) As Point()
        Dim Felder(Länge, Länge) As Integer
        Dim Punkte(Felder.Length - 1) As Point
        Dim I As Integer = 0
        Dim S As Integer = Math.DivRem(Länge, 2, 0)
        For X = -S To S
            For Y = -S To S
                Punkte(I) = New Point(X, Y)
                I += 1
            Next
        Next
        Return Punkte
    End Function
    ''' <summary>
    ''' Konvertiert einen String in eine Zahl
    ''' </summary>
    ''' <param name="Zahl"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CZahl(ByVal Zahl As String) As Integer
        Return CInt(Zahl)
    End Function
    ''' <summary>
    ''' Gibt die KP von 0-10 zurück berechnet aus 0-100.
    ''' </summary>
    ''' <param name="Leben"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function getKP(ByVal Leben As Single) As Integer
        Dim I As Integer = Math.DivRem(CInt(Leben), 10, 0)
        If I < 0 Then
            Return 0
        End If
        If Leben - I * 10 > 0 Then
            I += 1
        End If
        Return I
    End Function
    ''' <summary>
    ''' Teilt zwei Zahlen und gibt immer aufgerundete Werte zurück
    ''' </summary>
    ''' <param name="Nenner"></param>
    ''' <param name="Zähler"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Divi(ByVal Nenner As Integer, ByVal Zähler As Integer) As Integer
        Dim R As Integer
        Dim Z As Integer = Math.DivRem(Nenner, Zähler, R)
        If R > 0 Then Z += 1
        Return Z
    End Function
    ''' <summary>
    ''' Gibt für NUDS die Vergrößerung ihres Wertes zurück
    ''' </summary>
    ''' <param name="Wert"></param>
    ''' <param name="Nährungsschritte"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Round(ByVal Wert As Integer, ByVal Nährungsschritte As Integer) As Integer
        Dim R As Integer
        Dim Rückgabe As Integer = Math.DivRem(Wert, Nährungsschritte, R)
        Rückgabe *= Nährungsschritte
        If Nährungsschritte / 2 < R Then
            Rückgabe += Nährungsschritte
        End If
        Return Rückgabe
    End Function

    Public Shared Function CPoint(ByVal P As String) As Point
        Dim P2() As String = P.Split(",")
        Dim P1 As String = P2(1).Remove(0, 2)

        Return New Point(P2(0).Remove(0, 3), P1.Remove(P1.Length - 1))
    End Function

    Public Shared Function ToIntArry(ByVal Arra As String()) As Integer()
        If Arra(0) <> "" Or Arra.Length > 1 Then
            Dim Int(Arra.Length - 1) As Integer
            For I = 0 To Arra.Length - 1
                If Arra(I) = "" Then
                    Int(I) = 0
                Else
                    Int(I) = Arra(I)
                End If
            Next
            Return Int
        Else
            Return New Integer() {}
        End If

    End Function
    Public Shared Function ToDecArry(ByVal Arra As String()) As Decimal()
        If Arra(0) <> "" Or Arra.Length > 1 Then
            Dim Int(Arra.Length - 1) As Decimal
            For I = 0 To Arra.Length - 1
                If Arra(I) = "" Then
                    Int(I) = 0
                Else
                    Int(I) = Arra(I)
                End If
            Next
            Return Int
        Else
            Return New Decimal() {}
        End If
    End Function


    Public Shared Function FarbtabelleToString(ByVal B As Bitmap) As String
        Dim S As String = ""
        For I = 0 To B.Width - 1
            If I = B.Width - 1 Then
                S += B.GetPixel(I, 0).ToArgb.ToString
            Else
                S += B.GetPixel(I, 0).ToArgb.ToString + "_"
            End If
        Next
        Return S
    End Function
    Public Shared Function StringToFarbtabelle(ByVal S() As String) As Image
        Dim B As New Bitmap(S.Length, 1, Drawing.Imaging.PixelFormat.Format24bppRgb)
        For I = 0 To S.Length - 1
            B.SetPixel(I, 0, Color.FromArgb(CInt(S(I))))
        Next
        Return B
    End Function

    Public Shared Function Distance(ByVal P1 As Point, ByVal P2 As Point) As Integer
        Return Math.Abs(P1.X - P2.X) + Math.Abs(P1.Y - P2.Y)
    End Function
    Public Shared Function Distance(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
        Return Math.Abs(X1 - X2) + Math.Abs(Y1 - Y2)
    End Function

    Public Shared Function IntArrayToStr(ByVal IntArra As Integer()) As String
        Dim S As String = ""
        For I = 0 To IntArra.Length - 1
            If IntArra.Length - 1 = I Then
                S += IntArra(I)
            Else
                S += IntArra(I) + "_"
            End If
        Next
        Return S
    End Function
    Public Shared Function ToSTR(ByVal Arra() As Object) As String
        Dim S As String = ""
        If Arra(0) <> "" Or Arra.Length > 1 Then
            For I = 0 To Arra.Length - 1
                If S = Arra.Length - 1 Then
                    S += Arra.ToString
                Else
                    S += Arra.ToString + "|"
                End If
            Next
        End If
        Return S
    End Function
    Public Shared Function ToSTR(ByVal Arra() As ObjektSpielfeld.Wetterarten) As String
        Dim S As String = ""
        For I = 0 To Arra.Length - 1
            If S = Arra.Length - 1 Then
                S += Arra.ToString
            Else
                S += Arra.ToString + "|"
            End If
        Next
        Return S
    End Function
End Class
